﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DbMapping;
using DbMapping.Common;
using VideoCache.Server.DAL;
using VideoCache.Server.Model;

namespace VideoCache.Server.BLL
{
    public partial class UserBLL
    {
        public static int CreateUser(UserInfo info, string[] roles)
        {
            List<int> roleIds = new List<int>();
            foreach (string role in roles)
            {
                Conditions cons = new Conditions();
                cons.Add(new ConditionItem(LinkType.And, RoleInfo.NameField, CompareType.Equal, role));

                RoleInfo roleInfo = RoleBLL.Find(cons, null);
                if (roleInfo == null) throw new ArgumentException("指定的角色名称不存在：" + roleInfo.Name);

                roleIds.Add(roleInfo.Id);
            }

            using (IDbConnection conn = DbAccess.GetConnection())
            {
                conn.Open();
                using (IDbTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        info.Id = UserDAL.Add(tran, info);

                        foreach (int roleId in roleIds)
                        {
                            UserRoleInfo userRole = new UserRoleInfo();
                            userRole.UserId = info.Id;
                            userRole.RoleId = roleId;

                            UserRoleDAL.Add(tran, userRole);
                        }

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw new Exception("创建用户失败。", ex);
                    }

                    return info.Id;
                }
            }
        }

        public static bool IsUserInRole(string username, string role)
        {
            return true;
        }

        public static bool ValidateUser(string username, string password)
        {
            Conditions conditions = new Conditions();
            conditions.Add(new ConditionItem(LinkType.And, UserInfo.UserNameField, CompareType.Equal, username));

            UserInfo info = UserDAL.Find(conditions, null);

            if (info == null) return false;

            return info.Password == password;
        }

        public static UserInfo GetUserByName(string username)
        {
            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, UserInfo.UserNameField, CompareType.Equal, username));

            UserInfo info = UserDAL.Find(cons, null);

            if (info == null) throw new ArgumentException("指定的用户名不存在。");

            return info;
        }

        public static void CreateCompany(UserInfo user, AgentInfo agent)
        {
            using (IDbConnection conn = DbAccess.GetConnection())
            {
                conn.Open();
                using (IDbTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        Conditions conditions = new Conditions();
                        conditions.Add(new ConditionItem(LinkType.And, UserInfo.UserNameField, CompareType.Equal, user.UserName));
                        if (UserDAL.Exists(conditions)) throw new Exception("用户已经存在。");

                        conditions[0].Field = UserInfo.EmailField;
                        conditions[0].Value = user.Email;
                        if (UserDAL.Exists(conditions)) throw new Exception("地址邮件地址已被使用。");

                        user.RegisterTime = DateTime.Now;

                        int userId = UserDAL.Add(tran, user);

                        agent.UserId = userId;

                        AgentDAL.Add(tran, agent);

                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        throw new Exception("创建网吧失败。", ex);
                    }
                }
            }
        }
    }
}